//
//  HamatonDeviceConnectedView.swift
//  hamaton-app
//
//  Created by intyt on 2024/4/2.
//

import UIKit
import SnapKit

internal protocol HamatonDeviceConnectedViewDelegate: NSObjectProtocol {
    /// 修改设备名称
    func editDeviceNameAlert()
    // 车辆设置
    func carSettingViewShow()
    /// 设备诊断
    func diagnosisAlertShow()
    // 车辆轮胎设置
    func carTyreViewShow()
    // 固件升级
    func firmwareUpgradeViewShow()
    // 车辆轮胎展示
    func carTyreDisplayViewShow()
    // 显示器设置
    func xsqDisplayViewShow()
    
}

class HamatonDeviceConnectedView: UIView {
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setupUI()
    }
    internal weak var delegate: HamatonDeviceConnectedViewDelegate?
    func setupUI() {
        backgroundColor = .clear
        addSubview(titleLabel)
        addSubview(lineViewTop)
        addSubview(deviceName)
        addSubview(deviceNameEdit)
        addSubview(deviceCode)
        addSubview(setBtn)
        addSubview(carBtn)
        addSubview(displayBtn)
        addSubview(upgradesBtn)
        addSubview(diagnosisBtn)
        addSubview(XsqDisplayBtn)
        addSubview(XsqUpgradesBtn)
        
        titleLabel.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.left.equalTo(16)
            make.right.equalToSuperview()
            make.height.equalTo(30)
        }
        lineViewTop.snp.makeConstraints { (make) in
            make.top.equalTo(42)
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.height.equalTo(1)
        }
        deviceName.snp.makeConstraints { (make) in
            make.top.equalTo(lineViewTop.snp.bottom).offset(16)
            make.left.equalTo(16)
            make.right.equalToSuperview()
            make.height.equalTo(24)
        }
        deviceNameEdit.snp.makeConstraints{ (make) in
            make.top.equalTo(lineViewTop.snp.bottom).offset(16)
            make.right.equalTo(-16)
            make.height.equalTo(24)
            make.width.equalTo(24)
        }
        deviceCode.snp.makeConstraints { (make) in
            make.top.equalTo(deviceName.snp.bottom).offset(10)
            make.left.equalTo(16)
            make.right.equalToSuperview()
            make.height.equalTo(24)
        }
        setBtn.snp.makeConstraints { (make) in
            make.top.equalTo(deviceCode.snp.bottom).offset(68)
            make.centerX.equalToSuperview()
            make.width.equalTo(300)
            make.height.equalTo(42)
        }
        carBtn.snp.makeConstraints { (make) in
            make.top.equalTo(setBtn.snp.bottom).offset(18)
            make.centerX.equalToSuperview()
            make.width.equalTo(300)
            make.height.equalTo(42)
        }
        displayBtn.snp.makeConstraints { (make) in
            make.top.equalTo(carBtn.snp.bottom).offset(18)
            make.centerX.equalToSuperview()
            make.width.equalTo(300)
            make.height.equalTo(42)
        }
        upgradesBtn.snp.makeConstraints { (make) in
            make.top.equalTo(displayBtn.snp.bottom).offset(18)
            make.centerX.equalToSuperview()
            make.width.equalTo(300)
            make.height.equalTo(42)
        }
        diagnosisBtn.snp.makeConstraints { (make) in
            make.top.equalTo(upgradesBtn.snp.bottom).offset(18)
            make.centerX.equalToSuperview()
            make.width.equalTo(300)
            make.height.equalTo(42)
        }
        // 显示器
        XsqDisplayBtn.snp.makeConstraints { (make) in
            make.top.equalTo(deviceCode.snp.bottom).offset(68)
            make.centerX.equalToSuperview()
            make.width.equalTo(300)
            make.height.equalTo(42)
        }
        XsqUpgradesBtn.snp.makeConstraints { (make) in
            make.top.equalTo(setBtn.snp.bottom).offset(18)
            make.centerX.equalToSuperview()
            make.width.equalTo(300)
            make.height.equalTo(42)
        }
        
    }
    
    lazy var titleLabel: UILabel = {
        let label = UILabel(frame: .zero)
        //label.font = Theme.Font.size(54, bold: true)
        label.textColor = UIColor(hex6: 0x333333)
        label.numberOfLines = 1
        label.text = "已连接设备"
        return label
    }()
    
    lazy var lineViewTop: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex6: 0xF1F1F1)
        return view
    }()
    
    // 设备名称
    lazy var deviceName: UILabel = {
        let label = UILabel(frame: .zero)
        label.font = Theme.Font.size(18, bold: true)
        label.numberOfLines = 1
        label.text = "设备名称"
        return label
    }()
    
    // 设备名称编辑按钮
    lazy var deviceNameEdit: UIImageView = {
        let image = UIImage(named: "edit")
        let imageView = UIImageView(image: image)
        // 添加点击手势识别器
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(onClickDeviceEdit))
        imageView.addGestureRecognizer(tapGesture)
        imageView.isUserInteractionEnabled = true // 必须设置为 true，才能接收用户交互事件
        return imageView
    }()
    
    // 设备编号
    lazy var deviceCode: UILabel = {
        let label = UILabel(frame: .zero)
        label.textColor = UIColor(hex6: 0x333333)
        label.numberOfLines = 1
        label.text = "设备编号"
        return label
    }()
    // 设置按钮
    lazy var setBtn: UIButton = {
        let button = UIButton(type: .custom)
        //button.backgroundColor = .red
        button.setTitle(NSLocalizedString("系统参数设置", tableName: "HamatonLocalizable", comment: ""), for: .normal)
        button.setTitleColor(UIColor(hex6: 0xFFFFFF, alpha: 1.0), for: .normal)
        button.backgroundColor  = Theme.Color.Background.main
        button.layer.masksToBounds = true
        button.layer.cornerRadius = 5
        button.addTarget(self, action: #selector(onClickSetButton), for: .touchUpInside)
        return button
    }()
    // 车辆按钮
    lazy var carBtn: UIButton = {
        let button = UIButton(type: .custom)
        button.backgroundColor = .clear
        button.setTitle(NSLocalizedString("轮胎参数设置", tableName: "HamatonLocalizable", comment: ""), for: .normal)
        button.setTitleColor(UIColor(hex6: 0xFFFFFF, alpha: 1.0), for: .normal)
        button.backgroundColor  = Theme.Color.Background.maintwo
        button.layer.masksToBounds = true
        button.layer.cornerRadius = 5
        button.addTarget(self, action: #selector(onClickCarButton), for: .touchUpInside)
        return button
    }()
    // 展示按钮
    lazy var displayBtn: UIButton = {
        let button = UIButton(type: .custom)
        button.backgroundColor = .clear
        button.setTitle(NSLocalizedString("展示", tableName: "HamatonLocalizable", comment: ""), for: .normal)
        button.setTitleColor(UIColor(hex6: 0xFFFFFF, alpha: 1.0), for: .normal)
        button.backgroundColor  = Theme.Color.Background.mainthree
        button.layer.masksToBounds = true
        button.layer.cornerRadius = 5
        button.addTarget(self, action: #selector(onClickDisplayButton), for: .touchUpInside)
        return button
    }()
    // 固件升级按钮
    lazy var upgradesBtn: UIButton = {
        let button = UIButton(type: .custom)
        button.backgroundColor = .clear
        button.setTitle(NSLocalizedString("固件升级", tableName: "HamatonLocalizable", comment: ""), for: .normal)
        button.setTitleColor(UIColor(hex6: 0xFFFFFF, alpha: 1.0), for: .normal)
        button.backgroundColor  = Theme.Color.Background.success
        button.layer.masksToBounds = true
        button.layer.cornerRadius = 5
        button.addTarget(self, action: #selector(onClickUpgradesButton), for: .touchUpInside)
        return button
    }()
    // 诊断按钮
    lazy var diagnosisBtn: UIButton = {
        let button = UIButton(type: .custom)
        button.backgroundColor = .clear
        button.setTitle(NSLocalizedString("诊断", tableName: "HamatonLocalizable", comment: ""), for: .normal)
        button.setTitleColor(UIColor(hex6: 0xFFFFFF, alpha: 1.0), for: .normal)
        button.backgroundColor  = Theme.Color.Background.warn
        button.layer.masksToBounds = true
        button.layer.cornerRadius = 5
        button.addTarget(self, action: #selector(onClickDiagnosisButton), for: .touchUpInside)
        return button
    }()
    
    // 显示器展示按钮
    lazy var XsqDisplayBtn: UIButton = {
        let button = UIButton(type: .custom)
        button.backgroundColor = .clear
        button.setTitle(NSLocalizedString("显示器设置", tableName: "HamatonLocalizable", comment: ""), for: .normal)
        button.setTitleColor(UIColor(hex6: 0xFFFFFF, alpha: 1.0), for: .normal)
        button.backgroundColor  = Theme.Color.Background.mainthree
        button.layer.masksToBounds = true
        button.layer.cornerRadius = 5
        button.addTarget(self, action: #selector(onClickXsqDisplayButton), for: .touchUpInside)
        return button
    }()
    // 显示器固件升级按钮
    lazy var XsqUpgradesBtn: UIButton = {
        let button = UIButton(type: .custom)
        button.backgroundColor = .clear
        button.setTitle(NSLocalizedString("固件升级", tableName: "HamatonLocalizable", comment: ""), for: .normal)
        button.setTitleColor(UIColor(hex6: 0xFFFFFF, alpha: 1.0), for: .normal)
        button.backgroundColor  = Theme.Color.Background.success
        button.layer.masksToBounds = true
        button.layer.cornerRadius = 5
        button.addTarget(self, action: #selector(onClickUpgradesButton), for: .touchUpInside)
        return button
    }()
    
    // 编辑设备名称
    @objc func onClickDeviceEdit() {
        print("编辑设备名称")
        delegate?.editDeviceNameAlert()
    }
    
    // 设置操作
    @objc func onClickSetButton() {
        print("onClickButton")
        print("设置操作")
        delegate?.carSettingViewShow()
         
    }
    
    // 轮胎参数设置
    @objc func onClickCarButton() {
        print("轮胎参数设置")
        delegate?.carTyreViewShow()
    }
    
    // 展示操作
    @objc func onClickDisplayButton() {
        print("展示操作")
        delegate?.carTyreDisplayViewShow()
    }
    
    // 固件升级操作
    @objc func onClickUpgradesButton() {
        print("固件升级操作")
        delegate?.firmwareUpgradeViewShow()
    }
    
    // 诊断操作
    @objc func onClickDiagnosisButton() {
        print("诊断操作")
        delegate?.diagnosisAlertShow()
    }
    
    // 显示器设置
    @objc func onClickXsqDisplayButton() {
        print("显示器设置")
        delegate?.xsqDisplayViewShow()
    }
    
    
}
